/**
 * 
 * copy to http://www.javaportal.ru/java/articles/javaconfigprograms.html#xmlsample
 */

package cav.aescore;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.io.InputStreamReader;

public class IniFiles {
	Properties iniProperty = new Properties();
	public IniFiles(File f) throws IOException { this( f.getPath() ); }
	public IniFiles(String fname) throws IOException {
		loadFile( fname );
	}
	private void loadFile( String fname ) throws IOException
	 {
	  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fname)));

	  try
	  {
	   String section = "";
	   String line;

	   while( (line = br.readLine())!=null )
	   {
	    if( line.startsWith(";") ) continue;
	    if( line.startsWith("[") )
	    {
	     section = line.substring(1,line.lastIndexOf("]")).trim();
	     continue;
	    }
	    addProperty(section,line);
	   }
	  }
	  finally { br.close(); }
	 }
	
	 private void addProperty(String section,String line){
		 int equalIndex = line.indexOf("=");

		  if( equalIndex > 0 )
		  {
		   String name = section+'.'+line.substring(0,equalIndex).trim();
		   String value = line.substring(equalIndex+1).trim();
		   iniProperty.put(name,value);
		  }
	 }
	 public String getProperty(String section,String var,String def)
	 {
	  return iniProperty.getProperty(section+'.'+var,def);
	 }

	 public int getProperty(String section,String var,int def)
	 {
	  String sval = getProperty(section,var,Integer.toString(def));
	  
	  return Integer.decode(sval).intValue();
	 }

	 public boolean getProperty(String section,String var,boolean def)
	 {
	  String sval = getProperty(section,var,def ? "True":"False");

	  return sval.equalsIgnoreCase("Yes") || 
	           sval.equalsIgnoreCase("True");
	 }		 
	
}
